import sys
from typing import List


def func(nums: List, money):
    temp = sorted(nums, key=lambda x: x[2] / x[0], reverse=True)

    result = 0

    for i in range(N):
        while money >= temp[i][0]:
            if temp[i][1] == 0:
                break
            for j in range(temp[i][1]):
                temp[i][1] -= 1
                result += temp[i][2]
                money -= temp[i][0]
    return result


if __name__ == '__main__':
    line_1 = list(map(int, sys.stdin.readline().strip().split(" ")))
    X, N = line_1[0], line_1[1]

    commodity = []
    for _ in range(N):
        line = list(map(int, sys.stdin.readline().strip().split(" ")))
        commodity.append(line)

    print(func(commodity, X))
